/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.lym.common.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

import net.sf.json.JSONObject;

import java.lang.reflect.Type;

/**
 * Json工具类.
 * @author liyingming
 * @Jautodoc 荒墨丶迷失  百度AI社区版主 
 */
public class GsonUtils {
    private static Gson gson = new GsonBuilder().create();

    public static String toJson(Object value) {
        return gson.toJson(value);
    }

    public static <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
        return gson.fromJson(json, classOfT);
    }

    public static <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
        return (T) gson.fromJson(json, typeOfT);
    }
    
    /**
     * 将Map转换成JSON
     * 
     * @author liyingming
     * @date 2017-12-29
     * @param object
     *            需要转化为json的map对象
     * @return
     */
    public static String mapToJson(Object object) {
        JSONObject jsonObject = JSONObject.fromObject(object);
        return jsonObject.toString();
    }

}
